package cc.ttiot.utils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @Description: TODO
 * @Author WangSuheng
 * @Date 2021/10/2 1:06
 * @Version 1.0
 */
public class TimeUtils {
    //格式化日期：yyyy-MM-dd hh:mm:ss.SSS    毫秒级别
    public static final DateTimeFormatter millisecondFormatter;

    static {
        millisecondFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");  //HH大写是24小时制，小写是12小时制
    }

    //返回String类型时间(精确到毫秒，例：2020-03-20 23:20:09.213)
    //LocalDateTime是不可变类，线程安全
    public static String getMillisecondTime() {
        return millisecondFormatter.format(LocalDateTime.now(ZoneId.of("+8")));  //东8区
    }

    //停顿微秒
    public static void busyWaitMicros(long micros) {
        long waitUntil = System.nanoTime() + (micros * 1_000);
        while (waitUntil > System.nanoTime()) ;
    }
}
